<?php

namespace frontend\controllers;

use common\models\Adminuser;
use common\models\Category;
use common\models\Contact;
use common\models\Cooperate;
use common\models\Praise;
use common\models\Userr;
use Yii;
use common\models\Post;
use common\models\PostSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

use common\models\Tag;
use common\models\Comment;
use common\models\User;

/**
 * PostController implements the CRUD actions for Post model.
 */
class PostController extends Controller
{
    public $added = 0; //0代表还没有新回复

    /**
     * @inheritdoc
     */
    public function behaviors()
    {


        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],


        ];
    }


    /**
     * Lists all Post models.
     * @return mixed
     */
    public function actionIndex()
    {

        $checkLogin = new CheckLogin();

//        if (empty($_GET['code']) && empty($_SESSION['users'])) {
//            $checkLogin->actionScopeRedirectUriAppid();
//        }
//
//        if (!empty($_GET['code']) && empty($_SESSION['users'])) {
//            $checkLogin->getAccesstion($_GET['code']);
//        }

        $this->layout = false;
        $tags = Tag::findTagWeights();
        $recentComments = Comment::findRecentComments();
        $searchModel = new PostSearch();

        $param = Yii::$app->request->get();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data = Post::find()->where(['status'=>2])->orderBy('create_time desc')->all();
        if (!empty($param['type'])) {
            $data = Post::find()->where(['category_id' => $param['type'],'status'=>2])->orderBy('create_time desc')->all();
        }

        if (!empty($param['new']) && $param['new'] == 'new'){
            $data = Post::find()->where(['status'=>2])->limit(3)->orderBy('create_time desc')->all();
        }
        //分类
        $typss = Category::find()->where(['status' => 1])->all();
        //sid

        $msg = 'oaCoe1TmcCiHiEVvvtz5Tn5nqYSU';
        $username = '用户zE2yJ';
        $encrypt = md5($msg.'123');
        $checkLogin->makeSession($encrypt,$username);



        $sid = $_SESSION['users']['sid'];
        $username = $_SESSION['users']['username'];

        return $this->render('sy', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'tags' => $tags,
            'recentComments' => $recentComments,
            'data' => $data,
            'typss' => $typss,
            'sid' => $sid,
            'username' => $username,
        ]);
    }




    public function actionDetail($id)
    {
        $param = Yii::$app->request->get();
        $check = new CheckLogin();
        if ($param) {
            $username = empty($param['username']) ? '' : $param['username'];
            $sid = empty($param['sid']) ? '' : $param['sid'];
            $user = Userr::find()->where(['username' => $username])->one();
            if (!$user) {
                $check->actionScopeRedirectUriAppid();
            }
            $meg = md5($user->openid . CheckLogin::$key);
            if ($meg != $sid) {
                $check->actionScopeRedirectUriAppid();
            }
        }

        $this->layout = false;
        $commot = Comment::find()->where(['post_id' => $id, 'status' => 2])->all();
        $list = [];
        //分类
        $typss = Category::find()->where(['status' => 1])->all();

        //点赞
        $num = Praise::find()->where(['post_id'=>$id])->count('id');

        $com['number'] = $num;



        foreach ($commot as $k => $v) {
            $arr = [];
            $arr['content'] = $v['content'];
            $arr['create_time'] = $v['create_time'];
            $userData = Userr::find()->where(['id' => $v['userid']])->one();

            $arr['name'] = $userData['username'];
            $arr['img'] = $userData['icon'];
            $list[] = $arr;
        }

        $data = Post::find()->where(['id' => $id])->one();
        $data['tags'] = explode(',', $data['tags']);

        //step3.传数据给视图渲染

        return $this->render('info', [
            'data' => $data,
            'list' => $list,
            'typss' => $typss,
            'sid' => $sid,
            'username' => $username,
            'com' =>$com,
            'id'=>$id,
        ]);

    }

    public function actionComment($id)
    {
        $param = Yii::$app->request->post();
        $check = new CheckLogin();
        $usernme = empty($param['username']) ? '' : $param['username'];
        $sid = empty($param['sid']) ? '' : $param['sid'];
        $user = Userr::find()->where(['username' => $usernme])->one();
        if (!$user) {
            $check->actionScopeRedirectUriAppid();
        }
        $meg = md5($user->openid . CheckLogin::$key);
        if ($meg != $sid) {
            $check->actionScopeRedirectUriAppid();
        }


        $commentModel = new Comment();
        $commentModel->userid = $user->id;

        //step2. 当评论提交时，处理评论
        if ($param) {
            $commentModel->content = $_POST['content'];
            $commentModel->status = 1;
            $commentModel->post_id = $id;
            $commentModel->save();
        }

        return $this->redirect(array('/post/detail', 'id' => $id,'sid'=>$sid,'username'=>$usernme));
    }

    public function actionZan()
    {
        $param = Yii::$app->request->get();
        $username = empty($param['username']) ? '' : $param['username'];
        $sid = empty($param['sid']) ? '' : $param['sid'];
        $user = Userr::find()->where(['username' => $username])->one();
        $meg = md5($user->openid . CheckLogin::$key);
        if ($meg != $sid) {
            return json_encode(['status' => 500,'msg'=>'信息校验异常，请重新进来操作']);
        }
        $praise = Praise::find()->where(['user_id'=>$user->id,'post_id'=>$param['id']])->one();
        if($praise){
            return json_encode(['status' => 400,'msg'=>'你已经点赞过了~~~']);
        }else{
            $praise = new Praise();
            $praise->post_id = $param['id'];
            $praise->user_id = $user->id;
            $praise->create_time = time();
            $praise->save();
            $num = Praise::find()->where(['post_id'=>$param['id']])->count();
            return json_encode(['status' => 200,'data'=>$num,'msg'=>'点赞成功啦~~~']);
        }

    }
    public function actionAboutme()
    {
        $this->layout = false;
        //分类
        $typss = Category::find()->where(['status' => 1])->all();
        return $this->render('about',['typss' => $typss]);
    }

    public function actionTogether()
    {
        $this->layout = false;
        //分类
        $typss = Category::find()->where(['status' => 1])->all();
        session_start();
        $sid = $_SESSION['users']['sid'];
        $username = $_SESSION['users']['username'];

        return $this->render('togeter',['typss' => $typss,'sid'=>$sid,'username'=>$username]);
    }

    //添加合作
    public function actionAdd()
    {
        $param = Yii::$app->request->post();
        if (empty($param['name'])){
            return json_encode(['status' => 400,'msg'=>'名称不能为空']);
        }
        if (empty($param['link'])){
            return json_encode(['status' => 400,'msg'=>'联系方式不能为空']);
        }
        if (empty($param['saytext'])){
            return json_encode(['status' => 400,'msg'=>'内容不能为空']);
        }
        $sid = empty($param['sid']) ? '' : $param['sid'];
        $user = Userr::find()->where(['username' => $param['username']])->one();
        $meg = md5($user->openid . CheckLogin::$key);
        if ($meg != $sid) {
            return json_encode(['status' => 500,'msg'=>'信息校验异常，请重新进来操作']);
        }
        $coo = new  Cooperate();
        $coo->name = $param['name'];
        $coo->link = $param['link'];
        $coo->openid = $user->openid;
        $coo->content = $param['saytext'];
        $coo->create_tm = time();
        $coo->save();
        return json_encode(['status' => 200,'msg'=>'留言添加成功啦~~~']);
    }

    //添加留言
    public function actionSpeak()
    {
        $param = Yii::$app->request->get();
        $username = empty($param['username']) ? '' : $param['username'];
        $sid = empty($param['sid']) ? '' : $param['sid'];
        $user = Userr::find()->where(['username' => $username])->one();
        $meg = md5($user->openid . CheckLogin::$key);
        if ($meg != $sid) {
            return json_encode(['status' => 500,'msg'=>'信息校验异常，请重新进来操作']);
        }
        if ($user->status == 2){
            return json_encode(['status' => 500,'msg'=>'你被拉进小黑屋了哦，说不了话了']);
        }


        $commentModel = new Comment();
        $commentModel->userid = $user->id;

        //step2. 当评论提交时，处理评论
        if ($param) {
            $commentModel->content = $param['content'];
            $commentModel->status = 1;
            $commentModel->post_id = $param['id'];
            $commentModel->save();
        }
        return json_encode(['status' => 200,'msg'=>'留言成功，我们审批后回发布出来~~']);

    }
    public function actionType(){

        $this->layout = false;
        $tags = Tag::findTagWeights();
        $recentComments = Comment::findRecentComments();
        $searchModel = new PostSearch();

        $param = Yii::$app->request->get();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data = Post::find()->where(['status'=>2])->orderBy('create_time desc')->all();
        if (!empty($param['type'])) {
            $data = Post::find()->where(['category_id' => $param['type'],'status'=>2])->orderBy('create_time desc')->all();
        }

        if (!empty($param['new']) && $param['new'] == 'new'){
            $data = Post::find()->where(['status'=>2])->limit(3)->orderBy('create_time desc')->all();
        }
        //分类
        $typss = Category::find()->where(['status' => 1])->all();
        //sid

        session_start();
        $sid = $_SESSION['users']['sid'];
        $username = $_SESSION['users']['username'];


        return $this->render('sy', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'tags' => $tags,
            'recentComments' => $recentComments,
            'data' => $data,
            'typss' => $typss,
            'sid' => $sid,
            'username' => $username,
        ]);
    }
    //意见
    public function actionSugg(){
        $this->layout = false;
        //分类
        $typss = Category::find()->where(['status' => 1])->all();
        session_start();
        $sid = $_SESSION['users']['sid'];
        $username = $_SESSION['users']['username'];

        return $this->render('sugg',['typss' => $typss,'sid'=>$sid,'username'=>$username]);

    }
    //添加意见
    public function actionAddsugg(){
        $param = Yii::$app->request->post();
        if (empty($param['saytext'])){
            return json_encode(['status' => 400,'msg'=>'内容不能为空']);
        }
        if (mb_strlen($param['saytext'])>100){
            return json_encode(['status' => 200,'msg'=>'亲，你输入的内容太多啦，100个字内哦']);
        }
        $sid = empty($param['sid']) ? '' : $param['sid'];
        $user = Userr::find()->where(['username' => $param['username']])->one();
        $meg = md5($user->openid . CheckLogin::$key);
        if ($meg != $sid) {
            return json_encode(['status' => 500,'msg'=>'信息校验异常，请重新进来操作']);
        }
         $coo = new  Contact();
        $coo->uid = $user->id;
        $coo->status = 0;
        $coo->subject = $param['saytext'];
        $coo->createtime = time();
        $coo->save();
        return json_encode(['status' => 200,'msg'=>'感觉你提出的宝贵意见，我们会认真对待的']);

    }



}
